package com.union.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.union.unionht.R;
import com.union.widget.time.ArrayWheelAdapter;
import com.union.widget.time.OnWheelChangedListener;
import com.union.widget.time.WheelView;


/**
 * 日期选择�?
 *
 * @author Administrator
 */
public class ContentSelectedDialog extends Dialog implements View.OnClickListener {
    /**
     * 自定义Dialog监听�?
     */
    public interface PriorityListener {
        /**
         * 回调函数，用于在Dialog的监听事件触发后刷新Activity的UI显示
         */
//		public void refreshPriorityUI(String year, String month, String day,
//				String hours, String mins);
        public void refreshPriorityUI(String content);
    }

    private PriorityListener lis;
    private boolean scrolling = false;
    private ArrayWheelAdapter adapter = null;
    private Button btn_sure = null;
    private Button btn_cancel = null;
    private String content = "男";
    private WheelView contentView = null;
    private static int theme = R.style.myDialog;// 主题
    private LinearLayout date_layout;
    private TextView title_tv;
    private String title;
    private OnClickCallBack onClickCallBack;
    private String[] data;
    private Context context;
    private int visiableCount;

    public ContentSelectedDialog(final Context context,
                                 String content, String title, String[] data, int visiableCount, final PriorityListener listener) {
        super(context, theme);
        this.context = context;
        lis = listener;
        this.content = content;
        this.title = title;
        this.data = data;
        this.visiableCount = visiableCount;
    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public OnClickCallBack getOnClickCallBack() {
        return onClickCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_select_wheel);
        btn_sure = (Button) findViewById(R.id.confirm_btn);
        btn_sure.setOnClickListener(this);
        btn_cancel = (Button) findViewById(R.id.cancel_btn);
        btn_cancel.setOnClickListener(this);
        date_layout = (LinearLayout) findViewById(R.id.date_selelct_layout);
        DisplayMetrics dm = new DisplayMetrics();
        Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
        defaultDisplay.getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        System.out.println(width + "width" + height);
        LayoutParams lparams_hours = new LayoutParams(width, height / 3 + 10);
        date_layout.setLayoutParams(lparams_hours);
        title_tv = (TextView) findViewById(R.id.diaolog_title_tv);
        title_tv.setText(title);
        contentView = (WheelView) findViewById(R.id.content);

        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if (!scrolling) {
//					updateDays(yearview, monthview, dayview);
                }
            }
        };
        contentView.addChangingListener(listener);
        adapter = new ArrayWheelAdapter(data,data.length);
        contentView.setAdapter(adapter);
        contentView.setCurrentItem(0);// 传�?过去的是下标
        contentView.setCyclic(false);
        contentView.setVisibleItems(visiableCount);
    }


    public ContentSelectedDialog(Context context, PriorityListener listener) {
        super(context, theme);
        this.context = context;
    }

    public ContentSelectedDialog(Context context, String birthDate) {
        super(context, theme);
        this.context = context;

    }


    @Override
    public void onClick(View v) {

        if(v.getId()==R.id.confirm_btn){
            this.dismiss();
        }else {
            lis.refreshPriorityUI(adapter.getValues());
            this.dismiss();


            if (onClickCallBack != null) {
                onClickCallBack.confirm();
            }
        }
//        switch (v.getId()) {
//            case R.id.confirm_btn:
//                this.dismiss();
//                break;
//            case R.id.cancel_btn:
//                lis.refreshPriorityUI(adapter.getValues());
//                this.dismiss();
//
//
//                if (onClickCallBack != null) {
//                    onClickCallBack.confirm();
//                }
//                break;
//            default:
//                break;
//        }
    }

    public interface OnClickCallBack {
        public void confirm();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
